<?php

namespace App\Http\Controllers\Strator;

use App\Http\Controllers\Controller;
use App\Models\Strator\StratorProductSpec;
use App\Services\Strator\StratorProductSpecService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class StratorProductSpecController extends Controller
{
    protected Request $request;
    protected Model $model;

    public function __construct(Request $request,StratorProductSpec $stratorProductSpec)
    {
        $this->request=$request;
        $this->model = $stratorProductSpec;
    }

    //
    public function allInTree(StratorProductSpecService $productSpecService){
        $tree=$productSpecService->getStratorProductSpecs();
        return $this->success($tree);
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $perPage=$this->request->per_page??10;
        $list=$this->model->modelSearch($this->request,$this->model)
            ->with(['parent'])
            ->orderByDesc('id')
            ->paginate($perPage);
        return $this->success($list);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store()
    {
        $data=$this->request->only($this->storeOnly());
        $this->model->fill($data);
        $this->model->save();
        $this->saveOperationLog('添加产品规格');
        return $this->success(['id'=>$this->model->id]);
    }
    //
    public function storeOnly():array{
        return [
            'pid',
            'name',
            'spec_code',
            'spec_remark',
            'one_name',
            'two_name',
            'remark',
            'sort',
        ];
    }

    /**
     * Display the specified resource.
     */
    public function show()
    {
        $model=$this->model->findOrFail($this->request->id);
        return $this->success($model);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(StratorProductSpec $stratorProductSpec)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update()
    {
        $model=$this->model->findOrFail($this->request->id);
        $data=$this->request->except($this->updateExcept());
        $model->fill($data);
        $model->save();
        $this->saveOperationLog('修改产品规格');
        return $this->successMsg();
    }
    //
    public function updateExcept():array
    {
        return [
            'id',
        ];
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy()
    {
        $model=$this->model->findOrFail($this->request->id);
        $model->delete();
        $this->saveOperationLog('删除产品规格');
        return $this->successMsg('删除成功');
    }


}
